package com.ay.wiki_planet.service;

import com.ay.wiki_planet.domain.Content;
import com.ay.wiki_planet.domain.Doc;
import com.ay.wiki_planet.domain.DocExample;
import com.ay.wiki_planet.mapper.ContentMapper;
import com.ay.wiki_planet.mapper.DocMapper;
import com.ay.wiki_planet.req.DocQueryReq;
import com.ay.wiki_planet.req.DocSaveReq;
import com.ay.wiki_planet.resp.DocQueryResp;
import com.ay.wiki_planet.resp.PageResp;
import com.ay.wiki_planet.util.CopyUtil;
import com.ay.wiki_planet.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class DocService {

    @Resource
    private DocMapper docMapper;


    @Resource
    private SnowFlake snowFlake;


    @Resource
    private ContentMapper contentMapper;

    public PageResp<DocQueryResp> list(DocQueryReq req) {

        DocExample docExample = new DocExample();
        docExample.setOrderByClause("sort asc");
        DocExample.Criteria criteria = docExample.createCriteria();

        PageHelper.startPage(req.getPage(), req.getSize());
        List<Doc> docList = docMapper.selectByExample(docExample);


        PageInfo<Doc> pageInfo = new PageInfo<>(docList);
        log.info("总行数:{}", pageInfo.getTotal());
        log.info("总页数:{}", pageInfo.getPages());
        //pageInfo.getTotal(); 总行数
        //pageInfo.getPages(); 总页数

        //将List<Doc>变成List<DocResp>
        //List<DocResp> respList = new ArrayList<>();
        //for(Doc doc : docList){
        //    //实现对象的复制
        //    //DocResp docResp = new DocResp();
        //    //BeanUtils.copyProperties(doc,docResp);
        //
        //    DocResp docResp = CopyUtil.copy(doc, DocResp.class);
        //    respList.add(docResp);
        //}


        //列表复制
        List<DocQueryResp> list = CopyUtil.copyList(docList, DocQueryResp.class);

        PageResp<DocQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    /*
     * 查询全部数据单独接口 无需分页
     * */
    public List<DocQueryResp> all(Long ebookId) {

        DocExample docExample = new DocExample();
        docExample.createCriteria().andEbookIdEqualTo(ebookId);
        docExample.setOrderByClause("sort asc");
        DocExample.Criteria criteria = docExample.createCriteria();

        List<Doc> docList = docMapper.selectByExample(docExample);
        //列表复制
        List<DocQueryResp> list = CopyUtil.copyList(docList, DocQueryResp.class);
        return list;
    }

    /*
     * 保存
     * */
    public void save(DocSaveReq req) {
        Doc doc = CopyUtil.copy(req, Doc.class);
        //doc.id = content.id
        Content content = CopyUtil.copy(req, Content.class);
        if (ObjectUtils.isEmpty(req.getId())) {
            //新增
            doc.setId(snowFlake.nextId());
            docMapper.insert(doc);
            //内容新增
            content.setId(doc.getId());
            contentMapper.insert(content);
        } else {
            //更新
            docMapper.updateByPrimaryKey(doc);
            //blob富文本
            int count = contentMapper.updateByPrimaryKeyWithBLOBs(content);
            if (count == 0) {
                //如果数据为0执行插入
                contentMapper.insert(content);
            }
        }
    }

    /*
     * 删除
     * */
    public void delete(Long id) {
        docMapper.deleteByPrimaryKey(id);
    }

    public void delete(List<String> ids) {
        DocExample docExample = new DocExample();
        DocExample.Criteria criteria = docExample.createCriteria();
        criteria.andIdIn(ids);
        docMapper.deleteByExample(docExample);
    }


    /*文档内容的查询*/
    public String findContent(Long id) {
        Content content = contentMapper.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(content)) {
            return "";
        } else {
            return content.getContent();
        }
    }

}
